/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2025-07-05     86191       the first version
 */
#ifndef APPLICATIONS_SW420_H_
#define APPLICATIONS_SW420_H_

#include <rtthread.h>
#include <rtdevice.h>
#include "drv_gpio.h"
#include <stdlib.h>
/* 调试标签定义 */
#define DBG_TAG "sw420"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
/* Pin Definitions */
#define SW_PIN      GET_PIN(11, 4)   // Vibration sensor input

/* 线程优先级、栈大小和线程控制块定义 */
#define sw420_THREAD_PRIORITY       26
#define sw420_THREAD_STACK_SIZE     1024
#define sw420_THREAD_TIMESLICE      10

/* Vibration Data Structure */
typedef struct {
    uint32_t trigger_count;        // Interrupt trigger count
    uint32_t last_trigger_tick;    // Timestamp of last trigger
    uint32_t intensity;            // Vibration intensity (0-100)
    uint32_t sample_interval;      // 采样间隔 (ms)
    uint32_t window_size;          // 统计窗口大小 (ms)
} vibration_data_t;

extern vibration_data_t vib_data;

/* 平滑滤波缓冲区大小 */
#define SMOOTH_FILTER_SIZE 5
/* 历史触发计数缓冲区 */
#define TRIGGER_HISTORY_SIZE 10

/* 函数声明 */
void sw_interrupt_callback(void *args);
uint8_t map_intensity(uint32_t freq);
uint32_t apply_smoothing(uint32_t raw_value);
void intensity_sample_thread(void *parameter);
void cmd_vib_config(int argc, char *argv[]);
int vibration_monitor_init(void);


#endif /* APPLICATIONS_SW420_H_ */
